package cn.stronglink.assetmanage.business.inventory.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.entity.Inventory;
import cn.stronglink.assetmanage.listener.onRecyclerListener;

public class InventoryListWaitAdapter extends RecyclerView.Adapter<InventoryListWaitAdapter.ViewHolder>{

    private onRecyclerListener mListener;
    private Context mContext;
    private List<Inventory> mInventoryList;
    /**
     * 布局加载器
     */
    private LayoutInflater mLayoutInflater;
    public InventoryListWaitAdapter(Context context, List<Inventory> inventoryList){
        this.mContext = context;
        this.mInventoryList = inventoryList;
        this.mLayoutInflater = LayoutInflater.from(context);
    }

    public void setListener(onRecyclerListener listener) {
        mListener = listener;
    }



    @NonNull
    @Override
    public InventoryListWaitAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.item_inventory_list_wait, parent, false);
        InventoryListWaitAdapter.ViewHolder viewHolder = new InventoryListWaitAdapter.ViewHolder(view,mListener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull InventoryListWaitAdapter.ViewHolder holder, int position) {
        Inventory inventory = mInventoryList.get(position);
        holder.bindBean(inventory);
        if (position%2!=0){
            holder.item_inventory.setBackgroundColor(mContext.getResources().getColor(R.color.gray_check_item));
        }else {
            holder.item_inventory.setBackgroundColor(Color.WHITE);

        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mInventoryList == null ? 0 : mInventoryList.size();
    }



    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        private TextView tv_inventoryNo,tv_inventoryName,tv_inventory_create_time,
                item_inventory_create_person,item_inventory_plan_time,item_inventory_execute_person;
        private onRecyclerListener mListener;
        private LinearLayout item_inventory;

        public ViewHolder(View itemView ,onRecyclerListener listener) {
            super(itemView);

            tv_inventoryNo = itemView.findViewById(R.id.item_inventory_wait_no);
            tv_inventoryName = itemView.findViewById(R.id.item_inventory_wait_name);
            tv_inventory_create_time = itemView.findViewById(R.id.item_inventory_create_time);
            item_inventory_create_person = itemView.findViewById(R.id.item_inventory_create_person);
            item_inventory_plan_time = itemView.findViewById(R.id.item_inventory_plan_time);
            item_inventory_execute_person = itemView.findViewById(R.id.item_inventory_execute_person);
            item_inventory = itemView.findViewById(R.id.item_inventory);
            itemView.setOnClickListener(this);
            this.mListener = listener;

        }

        public void bindBean(Inventory inventory){
            tv_inventoryNo.setText(inventory.getId_());
            //todo：实体类待改
            tv_inventoryName.setText(inventory.getInventorySheetName());
            tv_inventory_create_time.setText(inventory.getCreateTime());
            item_inventory_create_person.setText(inventory.getCreateName());
            item_inventory_plan_time.setText(inventory.getExecuteTime());
            item_inventory_execute_person.setText(inventory.getOperatorName());
        }

        @Override
        public void onClick(View v) {
            if (mListener != null) {
                mListener.onItemClicked(v, getAdapterPosition());
            }
        }
    }


    public void notifyDatasChanged(List<Inventory> data) {
        this.mInventoryList = data;
        this.notifyDataSetChanged();
    }
}
